package org.zsk.cs.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.zsk.cs.domain.po.Forum;
import org.zsk.cs.domain.vo.ForumVo;
import org.zsk.cs.domain.vo.ForumVo;
import org.zsk.cs.response.ResultBody;
import org.zsk.cs.service.ForumService;
import org.zsk.fastdev.assign.Assign;

import java.util.List;

/**
 * <p>
 * 社区论坛 controller
 * </p>
 *
 * @author zsk
 * @version 1.0
 * Created on 2020/03/29
 */
@RestController
@RequestMapping("/forum")
public class ForumController {

    private final ForumService forumService;
    private final Assign assign;

    @Autowired
    public ForumController(ForumService forumService, Assign assign) {
        this.forumService = forumService;
        this.assign = assign;
    }

    @PostMapping("/a")
    public ResultBody add(@RequestBody ForumVo forumVo) throws Exception {
        Forum forum=new Forum();
        assign.assign(forumVo,forum,"$title,$districtHeadId");
        forumService.save(forum);
        return ResultBody.success();
    }

    @PutMapping("/u")
    public ResultBody update(@RequestBody ForumVo forumVo) throws Exception {
        Forum forum=new Forum();
        assign.assign(forumVo,forum);
        forumService.updateById(forum);
        return ResultBody.success();
    }

    @DeleteMapping("/d/{ids}")
    public ResultBody delete(@PathVariable List<Integer> ids) throws Exception {
        forumService.removeByIds(ids);
        return ResultBody.success();
    }

    @GetMapping("/g")
    public ResultBody page(ForumVo forumVo) throws Exception {
        Page<Forum> page=new Page<>();
        assign.assign(forumVo,page,"$current,$size");
        return ResultBody.success(forumService.page(page));
    }

    @GetMapping("/search")
    public ResultBody search(ForumVo forumVo) throws Exception {
        return ResultBody.success(forumService.search(forumVo));
    }

    @GetMapping("/getWithUserNameByPage")
    public ResultBody getWithUserNameByPage(ForumVo forumVo) throws Exception {
        return ResultBody.success(forumService.getWithUserNameByPage(forumVo));
    }

    @GetMapping("/districtHeadGetWithUserNameByPage")
    public ResultBody districtHeadGetWithUserNameByPage(ForumVo forumVo) throws Exception {
        return ResultBody.success(forumService.districtHeadGetWithUserNameByPage(forumVo));
    }

    @GetMapping("/residentGetWithUserNameByPage")
    public ResultBody residentGetWithUserNameByPage(ForumVo forumVo) throws Exception {
        return ResultBody.success(forumService.residentGetWithUserNameByPage(forumVo));
    }

    @PostMapping("/districtHeadSave")
    public ResultBody districtHeadSave(@RequestBody ForumVo forumVo) throws Exception {
        forumService.districtHeadSave(forumVo);
        return ResultBody.success();
    }

}
